﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.DesignPattern;
using Pixysoft.Framework.Noebe.MemoryCaches.Core;

namespace Pixysoft.Framework.Noebe.MemoryCaches
{
    public class CacheNoebeManager : Singleton<CacheNoebeManager>, ICacheNoebeManager
    {
        public static bool Initialize()
        {
            return _Instance.Initializes();
        }

        public static void StateSave()
        {
            CacheDataSet.Save();
        }



        public static string GLOBAL_FRAMEWORK_ID
        {
            get { return StringHelper.global_framework_id; }
        }


        public bool IsInitialized
        {
            get
            {
                return true;
            }
        }

        public ICacheSetting Setting
        {
            get
            {
                return SettingHelper.Configuration.Get(this.SingletonId);
            }
        }

        public System.Data.DataTable Select(string tableName)
        {
            return CacheDataSet.Instance.GetCacheTable(tableName);
        }

        public void Clear(string tableName)
        {
            CacheDataSet.Instance.ClearTable(tableName);
        }

        public void ClearAll()
        {
            CacheDataSet.Instance.ClearAll();

            CacheDataSet.Save();
        }




        public new INoebeManager this[string singleton]
        {
            get
            {
                return base[singleton];
            }
        }

        public bool Initializes()
        {
            SettingHelper.Load();

            CacheDataSet.Load();

            return true;
        }

        public bool Reloads()
        {
            return Initializes();
        }


        public System.Data.DataSet GetEntity()
        {
            return SettingHelper.GetManager(this.SingletonId).GetEntity();
        }

        public System.Data.DataTable GetEntity(string tableName)
        {
            return SettingHelper.GetManager(this.SingletonId).GetEntity(tableName);
        }

        public System.Data.DataSet GetStrongEntity()
        {
            return SettingHelper.GetManager(this.SingletonId).GetStrongEntity();
        }

        public System.Data.DataTable GetStrongEntity(string tableName)
        {
            return SettingHelper.GetManager(this.SingletonId).GetStrongEntity(tableName);
        }

        public bool ContainsEntity(string tableName)
        {
            return SettingHelper.GetManager(this.SingletonId).ContainsEntity(tableName);
        }

        public Pixysoft.Framework.XSchEmA.Entity.DatabaseSchema GetSchema()
        {
            return SettingHelper.GetManager(this.SingletonId).GetSchema();
        }


        public ICacheNoebeCommand CacheNoebeCommand
        {
            get { return new CacheAdvNoebeCommand(Setting); }
        }

        public INoebeCommand NoebeCommand
        {
            get { return new CacheAdvNoebeCommand(Setting); }
        }

        public IPagingCommand PagingCommand
        {
            get { return new CachePagingCommand(Setting); }
        }

        public IEntityNoebeCommand EntityNoebeCommand
        {
            get
            {
                return NoebeManager.GetEntityNoebeCommand(NoebeCommand);
            }
        }

        public IEntityPagingCommand EntityPagingCommand
        {
            get
            {
                return NoebeManager.GetEntityPagingCommand(PagingCommand);
            }
        }

        public IQuerion Querion
        {
            get { return new CacheAdvNoebeCommand(Setting); }
        }

        public ISession Session
        {
            get { return new CacheAdvNoebeCommand(Setting); }
        }
    }
}
